package com.kintreda.ieco_server.util;

import java.math.BigDecimal;

/**
 * BigDecimal 计算工具类
 * @author Administrator
 *
 */
public class Calcuator {

	private static final int DIV_SCALE_TWO = 2;

	private static final int DIV_ROUNDING_MODE = BigDecimal.ROUND_HALF_UP;

	private Calcuator() {

	}

	/***************************************
	 * 两数相加
	 * @param arg0
	 * @param arg1
	 * @return
	 */
	public static Double add(Object arg0, Object arg1) {
		
		BigDecimal num0 = new BigDecimal(arg0 == null ? "0" : arg0.toString());
		BigDecimal num1 = new BigDecimal(arg1 == null ? "0" : arg1.toString());
		return getFormatBigDecimal(num0.add(num1).setScale(DIV_SCALE_TWO, DIV_ROUNDING_MODE));
	}

	
	/***************************************
	 * 两数相减
	 * @param arg0
	 * @param arg1
	 * @return
	 */
	public static Double subtract(Object arg0, Object arg1) {
		
		BigDecimal num0 = new BigDecimal(arg0 != null ? arg0.toString() : "0");
		BigDecimal num1 = new BigDecimal(arg1 != null ? arg1.toString() : "0");
		return num0.subtract(num1).setScale(DIV_SCALE_TWO, DIV_ROUNDING_MODE).doubleValue();
	}
	
	
	/***************************************
	 * 两数相乘
	 * @param arg0
	 * @param arg1
	 * @return
	 */
	public static Double multiply(Object arg0, Object arg1) {
		
		BigDecimal num0 = new BigDecimal(arg0.toString());
		BigDecimal num1 = new BigDecimal(arg1.toString());
		return num0.multiply(num1).setScale(DIV_SCALE_TWO, DIV_ROUNDING_MODE).doubleValue();
	}

	
	/***************************************
	 * 两数相除
	 * @param arg0
	 * @param arg1
	 * @return
	 */
	public static Double divide(Object arg0, Object arg1) {
		
		if(arg0 == null || "".equals(arg0.toString())) {
			return 0.0;
		}
		
		if(arg1 == null || "".equals(arg1.toString()) || "0".equals(arg1.toString())) {
			return 0.0;
		}
		
		BigDecimal num0 = new BigDecimal(arg0.toString());
		BigDecimal num1 = new BigDecimal(arg1.toString());
		return num0.divide(num1, DIV_SCALE_TWO, DIV_ROUNDING_MODE).doubleValue();
	}

	
	/***************************************
	 * 获取格式化的数字
	 * @param arg0
	 * @return
	 */
	public static Double getFormatBigDecimal(Object arg0) {
		
		return new BigDecimal(arg0.toString()).setScale(DIV_SCALE_TWO, DIV_ROUNDING_MODE).doubleValue();
	}
	
	public static String formatFloatNumber(double value) {
        if(value != 0.00) {
            java.text.DecimalFormat df = new java.text.DecimalFormat("##########0.00");
            return df.format(value);
        } else {
            return "0.00";
        }
    }
	
	
	public static Double roundDivide(Object arg0, Object arg1, int scale,int roundingMode) {
		
		BigDecimal num0 = new BigDecimal(arg0.toString());
		BigDecimal num1 = new BigDecimal(arg1.toString());
		return num0.divide(num1, scale, roundingMode).doubleValue();
	}
	
	public static void main(String[] args) {
		
		System.out.println(Calcuator.formatFloatNumber(1));
	}
}
